package com.luxoft.scoremanager.server.dao.listimpl;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.luxoft.scoremanager.server.dao.ComponentValuesDao;
import com.luxoft.scoremanager.server.model.dao.ComponentValuesDaoModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;


public class ListComponentValuesDao implements ComponentValuesDao {
    private static List<ComponentValuesDaoModel> componentValuesList = new ArrayList<ComponentValuesDaoModel>();

    public ListComponentValuesDao() {
        init();
    }

    private void init() {
        componentValuesList.add(new ComponentValuesDaoModel(1, "Very weak", "Weak"));
        componentValuesList.add(new ComponentValuesDaoModel(2, "Weak", "Adequate"));
        componentValuesList.add(new ComponentValuesDaoModel(3, "Adequate", "Very weak"));
        componentValuesList.add(new ComponentValuesDaoModel(4, "Suspicious", "Adequate"));
        componentValuesList.add(new ComponentValuesDaoModel(5, "Very weak", "Adequate"));
        componentValuesList.add(new ComponentValuesDaoModel(6, "Weak", "Weak"));
        componentValuesList.add(new ComponentValuesDaoModel(7, "Adequate", "Moderate"));
        componentValuesList.add(new ComponentValuesDaoModel(8, "Adequate", "Weak"));
    }

    @Override
    public void addComponentValues(ComponentValuesDaoModel component) {
        checkNotNull(component);
        if (component.getId() == null) {
            component.setId((int) new Date().getTime());
        }
        componentValuesList.add(component);
    }

    @Override
    public void updateComponentValues(ComponentValuesDaoModel component) {
        checkNotNull(component);
        if (component.getId() == null) {
            component.setId((int) new Date().getTime());
            componentValuesList.add(component);
        } else {
            componentValuesList.remove(component);
            componentValuesList.add(component);
        }
    }

    @Override
    public void deleteComponentValues(ComponentValuesDaoModel component) {
        checkNotNull(component);
        checkNotNull(component.getId());

        componentValuesList.remove(component);
    }

    @Override
    public List<ComponentValuesDaoModel> getAll() {
        return componentValuesList;
    }

    @Override
    public List<ComponentValuesDaoModel> getAllForPackage(final int id) {
        checkNotNull(id);
        Collection<ComponentValuesDaoModel> filteredList = Collections2.filter(componentValuesList, new Predicate<ComponentValuesDaoModel>() {
            @Override
            public boolean apply(ComponentValuesDaoModel input) {
                return (id == (input.getScorePackageDao().getId()));
            }
        });
        return new ArrayList(filteredList);
    }

    @Override
    public ComponentValuesDaoModel getById(int id) {
        checkNotNull(id);
        for (ComponentValuesDaoModel component : componentValuesList) {
            if (id == component.getId()) {
                return component;
            }
            ;
        }
        return null;
    }
}
